﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrenzDenWebSite.Business;

namespace FrenzDenWebSite
{
    public partial class FriendnFamilyForm : OboutInc.oboutAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies[SiteConstants.COOKIESTRINGPROFILE] == null)
            {
                Response.Redirect("~/Default.aspx?prev=FriendnFamily&showmsg=1");
            }

            if (!IsPostBack)
            {
                GetProfileFamilyDetails();
                LoadRelationship();
                GetFamilyList();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string str = hfRelatedTo.Value;
        }

        private void LoadYear()
        {
            int endYear = DateTime.Now.Year - 10;
            int startYear = endYear - 80;

            for (int i = endYear; i >= startYear; i--)
            {
                //ddlYear.Items.Add(i.ToString());
            }
        }

        private void LoadRelationship()
        {
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                ddlFamilyRelation.DataSource = dataContext.FamilyMasters;
                ddlFamilyRelation.DataTextField = "Title";
                ddlFamilyRelation.DataValueField = "TitleID";
                ddlFamilyRelation.DataBind();
                ddlFamilyRelation.Items.Insert(0, "");
            }
        }

        public string GetRelatedProfile(long profileId)
        {
            string relatedProfile = string.Empty;
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                var result = dataContext.GetRelatedProfile(profileId);
                foreach (var item in result)
                {
                    relatedProfile = item.ProfileID.ToString() + "/" + item.FullName;
                }
            }

            return relatedProfile;
        }

        private void GetFamilyList()
        {
            long profileId = Convert.ToInt64(Request.Cookies[SiteConstants.COOKIESTRINGPROFILEID].Value);
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                GridFamilyList.DataSource = dataContext.GetProfileFamily(profileId);
                GridFamilyList.DataBind();
            }
        }

        private void GetProfileFamilyDetails()
        {
            long profileId = Convert.ToInt64(Request.Cookies[SiteConstants.COOKIESTRINGPROFILEID].Value);
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                var result = dataContext.GetProfileFamilyDetails(profileId);
                foreach (var item in result)
                {
                    ddlRelationStatus.SelectedValue = item.RelationshipStatusID.ToString();
                    ddlRelationshipPrivacy.SelectedValue = item.RelationshipStatusPrivacy.ToString();
                    ddlFamilyPrivacy.SelectedValue = item.FamilyPrivacy.Value.ToString();
                    ddlFriendPrivacy.SelectedValue = item.FriendsPrivacy.Value.ToString();
                }
            }
        }

        protected void ddlRelationStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlRelationStatus.SelectedValue == "2" || ddlRelationStatus.SelectedValue == "3" || ddlRelationStatus.SelectedValue == "4")
            {
                //trRelation.Style["display"] = "block";
                txtRelatedTo.Visible = true;
            }
            else
            {
                //trRelation.Style["display"] = "none";
                txtRelatedTo.Visible = false;
            }
        }

        public string SaveData(long profileID, short relationshipStatusId, short relationshipPrivacy, long relatedProfileID, short familyPrivacy, short friendPrivacy)
        {
            MessageBox msgbox = null;
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    long profileId = Convert.ToInt64(Request.Cookies[SiteConstants.COOKIESTRINGPROFILEID].Value);
                    dataContext.UpdateProfileFamily(profileId, relationshipStatusId, relationshipPrivacy, relatedProfileID, DateTime.Now, familyPrivacy, friendPrivacy);
                    msgbox = new MessageBox("Successfully updated!");
                }
            }
            catch
            {
                msgbox = new MessageBox(SiteConstants.ERRORMSG);
            }

            return msgbox.ReturnObject();
        }

        protected void btnAddFamily_Click(object sender, EventArgs e)
        {
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                long profileId = Convert.ToInt64(Request.Cookies[SiteConstants.COOKIESTRINGPROFILEID].Value);
                long familyProfileId = 0;
                if (!string.IsNullOrWhiteSpace(hfFamilyProfile.Value))
                {
                    familyProfileId = Convert.ToInt64(hfFamilyProfile.Value);
                }

                dataContext.AddProfileFamily(profileId, familyProfileId, Convert.ToInt16(ddlFamilyRelation.SelectedValue));
                GetFamilyList();
            }
        }

        protected void imgDeleteFamily_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}